package com.css.dzj.dzjap.modules.publick.controller;

import com.alibaba.fastjson.JSON;
import com.css.aq.server.service.BaseService;
import com.css.dzj.dzjap.common.audit.service.SysAuditService;
import com.css.dzj.dzjap.common.util.BeanUtil;
import com.css.dzj.dzjap.common.util.Constant;
import com.css.dzj.dzjap.common.util.R;
import com.css.dzj.dzjap.modules.enterprise.entity.ApEnterpriseCreditEntity;
import com.css.dzj.dzjap.modules.enterprise.service.EnterpriseJSService;
import com.css.dzj.dzjap.modules.enterprise.service.EnterpriseService;
import com.css.dzj.dzjap.modules.expertcredit.entity.ApExpertCreditEntity;
import com.css.dzj.dzjap.modules.expertcredit.service.ExpertCreditService;
import com.css.dzj.dzjap.modules.publick.entity.ApPublicEntity;
import com.css.dzj.dzjap.modules.publick.service.PublicService;
import com.css.dzj.dzjap.modules.todolist.entity.ApTodoEntity;
import com.google.gson.annotations.JsonAdapter;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/publick")
public class PublicController {
     //公开公告
    @Autowired
    private PublicService publicService;

    //专家信用信息表
    @Autowired
    private ExpertCreditService expertCreditService;

    //评价单位信用信息表
    @Autowired
    private EnterpriseService enterpriseService;

    //建设单位信用信用表
    @Autowired
    private EnterpriseJSService enterpriseJsService;

    @Autowired
    private SysAuditService sysAuditService;
    @Autowired
    HttpServletRequest request;


    /**
     * 通过主键Id查询公开公告表
     */
    @GetMapping(value = "/getPublick")
    public R getPublick(@RequestParam("publicId") String publicId){
        String s ="";
        try {
            s = JSON.toJSONString(publicId);
            sysAuditService.saveSuccessAudit("查询成功", Constant.PUBLICK, Constant.PT, Constant.SELECT, s, request);
            return R.ok().put("data", publicService.getPublick(publicId));
        }catch (Exception e){
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("查询失败", Constant.PUBLICK, Constant.PT, Constant.SELECT, s, e, request);
            return R.error(e.getMessage());
        }
    }


    /**
     * 添加公开公告表
     */
    @PostMapping(value = "/addPublick")
    public R addPublick(@RequestBody ApPublicEntity pub) {
        System.out.println(pub.getTitle());

        if (StringUtils.isEmpty(pub.getTitle()) || StringUtils.isEmpty(pub.getPublicType())
                || StringUtils.isEmpty(pub.getPublicState())) {

            return R.error("必填字段不能为空");
        }
        String flag = publicService.addPublick(pub);
        if (!"0".equals(flag)) {
            R r = new R();
            r.put("code", 200);
            r.put("msg", "操作成功");
            r.ok().put("data", flag);
            String s = JSON.toJSONString(pub);
            sysAuditService.saveSuccessAudit("添加成功", Constant.PUBLICK, Constant.PT, Constant.ADD, s, request);
            return r;
        } else {
            String   s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("添加失败", Constant.PUBLICK, Constant.PT, Constant.ADD, s, null, request);
            return R.error("添加失败");
        }


    }

    /**
     * 修改公开公告表
     */
    @PostMapping(value = "/updatePublick")
    public R updatePublick(@RequestBody ApPublicEntity pub ){
        String s = "";
        try {
            ApPublicEntity aPub = publicService.getPublick(pub.getPublicId());
            BeanUtil.copyBeanNotNullToBean(pub, aPub);
            publicService.updatePublic(aPub);
            s = JSON.toJSONString(pub);
            sysAuditService.saveSuccessAudit("修改公开公告表成功", Constant.PUBLICK, Constant.PT, Constant.UPDATE, s, request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("修改公开公告表失败", Constant.PUBLICK, Constant.PT, Constant.UPDATE, s, e, request);
            return R.error(e.getMessage());
        }
    }


    /**
     * 审批公开公告表
     */
    @PostMapping(value = "/approvalPublick")
    public R approvalPublick(@RequestBody ApPublicEntity pub ){
        String s = "";
        try {
            if(StringUtils.isEmpty(pub.getPublishType())){
                return  R.error("必选字段不能为空！");
            }
            ApPublicEntity aPub = publicService.getPublick(pub.getPublicId());
            BeanUtil.copyBeanNotNullToBean(pub, aPub);
            if("1".equals(aPub.getPublishType())){
                aPub.setPublishTime(new Date());
            }
            publicService.approvalPublick(aPub);
            s = JSON.toJSONString(pub);
            sysAuditService.saveSuccessAudit("审批公开公告表成功", Constant.PUBLICK, Constant.PT, Constant.APPROVALPUBLICK, s, request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("审批公开公告表失败", Constant.PUBLICK, Constant.PT, Constant.APPROVALPUBLICK, s, e, request);
            return R.error(e.getMessage());
        }
    }



    /**
     * 删除公开公告表
     */
    @PostMapping(value = "/deletePublick")
    public R deletePublick(@RequestParam("publicId") String publicId){
        String s = "";
        System.out.println(publicId);
        try{
            publicService.deletePublick(publicId);
            s = JSON.toJSONString(publicId);
            sysAuditService.saveSuccessAudit("公开公告删除成功", Constant.PUBLICK, Constant.PT, Constant.DEL, s, request);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("公开公告删除失败", Constant.PUBLICK, Constant.PT, Constant.DEL, s, e, request);
            return R.error(e.getMessage());
        }

    }


    /**
     * 分页查询所有公开公告表
     */
    @GetMapping(value = "/getAllPublick")
    public R getAllPublick(@RequestParam(required = false) Integer pageNo,
                           @RequestParam(required = false) Integer pageSize
                                                                       ) {
        String s = "";
        try{
            PageRequest pageRequest = PageRequest.of(pageNo-1, pageSize);

           /* QueryResults page = publicService.getAllPublick(pageRequest);
            List<ApPublicEntity> list = page.getResults();
            List<ApPublicEntity> newlist = new ArrayList<>();
            for (ApPublicEntity uplist : list) {
                ApPublicEntity pub  = new ApPublicEntity();
                pub.setPublicId(uplist.getPublicId());
                pub.setPublicState(uplist.getPublicState());
                pub.setDelFlag(uplist.getDelFlag());
                Date createTime =null;
                SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    createTime = sf.parse(uplist.getCreateTime().toString());
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                pub.setCreateTime(createTime);
                pub.setRevokeg(uplist.getRevokeg());
                pub.setContent(uplist.getContent());
                pub.setCreateUser(uplist.getCreateUser());
                pub.setEnclosure(uplist.getEnclosure());
                pub.setPublicType(uplist.getPublicType());
                pub.setTitle(uplist.getTitle());
                newlist.add(pub);
            }

            QueryResults qr  = new QueryResults(newlist,page.getLimit(),page.getOffset(),page.getTotal());*/
            QueryResults page = publicService.getAllPublick(pageRequest);
            HttpSession session= request.getSession();
            s = JSON.toJSONString(page);
            sysAuditService.saveSuccessAudit("查询成功", Constant.PUBLICK,Constant.PT,Constant.PAGE,s,request);
            return R.ok().put("data", page);
        }catch (Exception e){
            e.printStackTrace();
            sysAuditService.saveFailAudit("查询失败",Constant.PUBLICK,Constant.PT,Constant.PAGE,s,e,request);
            return  R.error("操作失败!");
        }
    }


    /**
     * 撤销公开公告表
     */
    @PostMapping(value = "/updateRePublick")
    public R updateRePublick(@RequestBody ApPublicEntity pub ){
        String s  = "";
        try {
            if("0".equals(pub.getPublicState())){
                return R.error("未公开状态，不能撤销!");
            }else {
                ApPublicEntity aPub = publicService.getPublick(pub.getPublicId());
                BeanUtil.copyBeanNotNullToBean(pub, aPub);
                aPub.setRevokeg(Constant.REVOKE_YES);
                aPub.setPublicState("0");
                publicService.update(aPub);
                s = JSON.toJSONString(aPub);
                sysAuditService.saveSuccessAudit("撤销成功", Constant.PUBLICK,Constant.PT,Constant.REVOKE,s,request);
                return R.ok();
            }
        } catch (Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("撤销失败",Constant.PUBLICK,Constant.PT,Constant.REVOKE,s,e,request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 多条件查询公开公告表
     */
    @GetMapping(value ="/getAllConPublick")
    public R getAllConPublick(@RequestParam String title,
                              @RequestParam(required = false) String createUser,
                              @RequestParam(required = false) String publicType,
                              @RequestParam(required = false) String publicState,
                              @RequestParam(required = false) Integer pageNo,
                              @RequestParam(required = false) Integer pageSize){

        String s = "";
        try {
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults page = publicService.getAllConPublick(pageRequest, title, createUser, publicType, publicState);
            HttpSession session= request.getSession();
            s = JSON.toJSONString(page);
            sysAuditService.saveSuccessAudit("查询成功", Constant.PUBLICK,Constant.PT,Constant.PAGE,s,request);
            return R.ok().put("data", page);
        }catch (Exception e){
            e.printStackTrace();
            sysAuditService.saveFailAudit("查询失败",Constant.PUBLICK,Constant.PT,Constant.PAGE,s,e,request);
            return  R.error("操作失败!");
        }
    }

    /**
     *  添加拟制公告信息通过关联专家ID
     */
    @PostMapping(value = "/addGlPublick")
    public R addGlPublick(@RequestBody ApPublicEntity pub) {
        String s = "";
        try{
            if (StringUtils.isEmpty(pub.getTitle()) || StringUtils.isEmpty(pub.getPublicType())
                    || StringUtils.isEmpty(pub.getPublicState())) {

                return R.error("必填字段不能为空");
            }
            ApExpertCreditEntity expertCred = expertCreditService.getExpertCred(pub.getRelationId());
            if(expertCred!=null){
                expertCred.setExpertNotice("1");
                expertCreditService.update(expertCred);
            }
            String flag = publicService.addGlPublick(pub);
            R r = new R();
            if (!"0".equals(flag)) {
                r.put("code", 200);
                r.put("msg", "操作成功");
                r.ok().put("data", flag);
            }
            s = JSON.toJSONString(pub);
            sysAuditService.saveSuccessAudit("添加成功", Constant.PUBLICK,Constant.PT,Constant.ADD,s,request);
            return r;
        }catch (Exception e){
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("添加失败", Constant.PUBLICK, Constant.PT, Constant.ADD, s, e, request);
            return R.error(e.getMessage());
        }
    }


    /**
     * 添加黑名单拟制公告信息通过关联专家ID
     */
    @PostMapping(value = "/addGlRePublick")
    public R addGlRePublick(@RequestBody ApPublicEntity pub) {
        System.out.println(pub.getRelationId());
        String s = "";
        try{
            if (StringUtils.isEmpty(pub.getTitle()) || StringUtils.isEmpty(pub.getPublicType())
                    || StringUtils.isEmpty(pub.getPublicState())) {

                return R.error("必填字段不能为空");
            }
            ApExpertCreditEntity expertCred = expertCreditService.getReExpertCred(pub.getRelationId());
            expertCred.setExpertNotice("1");
            expertCreditService.update(expertCred);

            String flag = publicService.addGlRePublick(pub);
            R r = new R();
            if (!"0".equals(flag)) {
                r.put("code", 200);
                r.put("msg", "操作成功");
                r.ok().put("data", flag);
            }
            s = JSON.toJSONString(expertCred);
            sysAuditService.saveSuccessAudit("添加成功", Constant.PUBLICK,Constant.PT,Constant.ADD,s,request);
            return r;
        }catch (Exception e){
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("添加失败", Constant.PUBLICK, Constant.PT, Constant.ADD, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 通过关联ID查询公开公告表
     */
    @GetMapping(value = "/getGlPublick")
    public R getGlPublick(@RequestParam String relationId){
        String s = "";
        try {
            ApPublicEntity glPublick = publicService.getGlPublick(relationId);
            s = JSON.toJSONString(glPublick);
            sysAuditService.saveSuccessAudit("查询成功", Constant.PUBLICK, Constant.PT, Constant.SELETE, s, request);
            return R.ok().put("data", glPublick);
        }catch (Exception e){
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("查询失败", Constant.PUBLICK, Constant.PT, Constant.SELETE, s, e, request);
            return R.error(e.getMessage());
        }
    }


    /**
     * 通过关联ID类型查询公开公告表
     */
    @GetMapping(value = "/getGlTYPublick")
    public R getGlTYPublick(@RequestParam String relationId,
                            @RequestParam String enterType){
        String s = "";
        try {
            ApPublicEntity glPublick = publicService.getGlTYPublick(relationId, enterType);
            s = JSON.toJSONString(glPublick);
            sysAuditService.saveSuccessAudit("查询成功", Constant.PUBLICK, Constant.PT, Constant.SELETE, s, request);
            return R.ok().put("data", glPublick);
        }catch (Exception e){
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("查询失败", Constant.PUBLICK, Constant.PT, Constant.SELETE, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     *  添加拟制公告信息通过关联评价单位ID
     */
    @PostMapping(value = "/addGlPjPublick/{enterpriseCredittype}")
    public R addGlPjPublick(@PathVariable String enterpriseCredittype, @RequestBody ApPublicEntity pub) {
        System.out.println(pub.getRelationId());
        String s = "";
        try{
            if (StringUtils.isEmpty(pub.getTitle()) || StringUtils.isEmpty(pub.getPublicType())
                    || StringUtils.isEmpty(pub.getPublicState())) {

                return R.error("必填字段不能为空");
            }
            ApEnterpriseCreditEntity enterPrise = enterpriseService.getEnterPrise(pub.getRelationId(),enterpriseCredittype);
            enterPrise.setEnterpriseNotice("1");
            enterpriseService.update(enterPrise);

            String flag = publicService.addGlPjPublick(pub,enterpriseCredittype);
            R r = new R();
            if (!"0".equals(flag)) {
                r.put("code", 200);
                r.put("msg", "操作成功");
                r.ok().put("data", flag);

            }
            s = JSON.toJSONString(enterPrise);
            sysAuditService.saveSuccessAudit("添加成功", Constant.PUBLICK,Constant.PT,Constant.ADD,s,request);
            return r;
        }catch (Exception e){
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("添加失败", Constant.PUBLICK, Constant.PT, Constant.ADD, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     *  添加黑名单拟制公告信息通过关联评价单位Id
     */
    @PostMapping(value = "/addGlPjRePublick/{enterpriseCredittype}")
    public R addGlPjRePublick(@PathVariable String enterpriseCredittype, @RequestBody ApPublicEntity pub) {
        System.out.println(pub.getRelationId());
        String s = "";
        try{
            if (StringUtils.isEmpty(pub.getTitle()) || StringUtils.isEmpty(pub.getPublicType())
                    || StringUtils.isEmpty(pub.getPublicState())) {

                return R.error("必填字段不能为空");
            }
            ApEnterpriseCreditEntity enterPrise = enterpriseService.getReEnterPrise(pub.getRelationId(),enterpriseCredittype);
            enterPrise.setEnterpriseNotice("1");
            enterpriseService.update(enterPrise);

            String flag = publicService.addGlPjRePublick(pub,enterpriseCredittype);
            R r = new R();
            if (!"0".equals(flag)) {
                r.put("code", 200);
                r.put("msg", "操作成功");
                r.ok().put("data", flag);

            }
            s = JSON.toJSONString(enterPrise);
            sysAuditService.saveSuccessAudit("添加成功", Constant.PUBLICK,Constant.PT,Constant.ADD,s,request);
            return r;
        }catch (Exception e){
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("添加失败", Constant.PUBLICK, Constant.PT, Constant.ADD, s, e, request);
            return R.error(e.getMessage());
        }
    }


    /**
     *  添加拟制公告信息通过关联建设单位ID
     */
    @PostMapping(value = "/addGlJsPublick/{enterpriseCredittype}")
    public R addGlJsPublick(@PathVariable String enterpriseCredittype, @RequestBody ApPublicEntity pub) {
        System.out.println(pub.getRelationId());
        String s = "";
        try{
            if (StringUtils.isEmpty(pub.getTitle()) || StringUtils.isEmpty(pub.getPublicType())
                    || StringUtils.isEmpty(pub.getPublicState())) {

                return R.error("必填字段不能为空");
            }
            ApEnterpriseCreditEntity enterPrise = enterpriseJsService.getEnterPrise(pub.getRelationId(),enterpriseCredittype);
            enterPrise.setEnterpriseNotice("1");
            enterpriseJsService.update(enterPrise);

            String flag = publicService.addGlJsPublick(pub,enterpriseCredittype);
            R r = new R();
            if (!"0".equals(flag)) {
                r.put("code", 200);
                r.put("msg", "操作成功");
                r.ok().put("data", flag);

            }
            s = JSON.toJSONString(enterPrise);
            sysAuditService.saveSuccessAudit("添加成功", Constant.PUBLICK,Constant.PT,Constant.ADD,s,request);
            return r;
        }catch (Exception e){
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("添加失败", Constant.PUBLICK, Constant.PT, Constant.ADD, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     *  添加黑名单拟制公告信息通过关联建设单位Id
     */
    @PostMapping(value = "/addGlJsRePublick/{enterpriseCredittype}")
    public R addGlJsRePublick(@PathVariable String enterpriseCredittype, @RequestBody ApPublicEntity pub) {
        System.out.println(pub.getRelationId());
        String s = "";
        try{
            if (StringUtils.isEmpty(pub.getTitle()) || StringUtils.isEmpty(pub.getPublicType())
                    || StringUtils.isEmpty(pub.getPublicState())) {

                return R.error("必填字段不能为空");
            }
            ApEnterpriseCreditEntity enterPrise = enterpriseJsService.getReEnterPrise(pub.getRelationId(),enterpriseCredittype);
            enterPrise.setEnterpriseNotice("1");
            enterpriseJsService.update(enterPrise);

            String flag = publicService.addGlJsRePublick(pub,enterpriseCredittype);
            R r = new R();
            if (!"0".equals(flag)) {
                r.put("code", 200);
                r.put("msg", "操作成功");
                r.ok().put("data", flag);

            }
            s = JSON.toJSONString(enterPrise);
            sysAuditService.saveSuccessAudit("添加成功", Constant.PUBLICK,Constant.PT,Constant.ADD,s,request);
            return r;
        }catch (Exception e){
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("添加失败", Constant.PUBLICK, Constant.PT, Constant.ADD, s, e, request);
            return R.error(e.getMessage());
        }
    }



    /**
     * 带有标题参数查询list
     * @author wt
     * @date 2020\3\10 0010
     */
    @GetMapping("/titleList")
    public List titleList(@RequestParam String title,@RequestParam String publicType,@RequestParam String isPublic){
        String s = "";
        try{
            List<ApPublicEntity> titleList =  publicService.titleList(title,publicType,isPublic);
            return titleList;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

    }


    /**
     * 不带参数查询list
     * @author wt
     * @date 2020\3\10 0010
     */
    @GetMapping("/allList")
    public List allList(@RequestParam String publicType,@RequestParam String isPublic){
        String s ="";
        try{
            List<ApPublicEntity> allList =  publicService.allList(publicType,isPublic);
            return allList;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 通过id查询详情
     * @author wt
     * @date 2020\3\12 0012
     */
    @GetMapping("/publicEntity")
    public ApPublicEntity getEntity(@RequestParam("id") String id){
        ApPublicEntity  publicEntity = publicService.getEntity(id);
        return publicEntity;
    }

    /**
     * 查询公开公告所有信息
     * 对外提供接口
    *@Author weitao
    *@Date 15:54 2020/6/29
    */
    @GetMapping(value ="/getApList")
    public List<ApPublicEntity> getApList(){

        try {
            List<ApPublicEntity> list = publicService.getApList();
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

}
